package batch.import_user;

import java.sql.*;
import java.io.*;

import org.apache.commons.lang.ObjectUtils;
import org.apache.log4j.*;

import batch.common.secui.*;
import batch.common.db.ConnectionManager;

public class makeUserFileForWelcomeMail
{
    private final String TRPLKEY     = "QWERTYUIOP!@#$%^&*()aaaa";

    private final String TABLE_NAME  = "G_GER_IMPORT_TEMP_2";
    private final String MEMBERGBN   = "10398101";
    private final String FILE_NAME   = "mail_ger_200605_Midao.txt";

    private final String REG_COUNTRY = "0102018";
    private final String STRURL      = "http://www.samsungmobile.com/interface/auto_login.jsp";

    public static void main(String[] args)
    {
        Logger.getLogger("import_user.makeUserFileForWelcomeMail").info("( * ) Start.");

        makeUserFileForWelcomeMail obj = new makeUserFileForWelcomeMail();
        obj.init();

        Logger.getLogger("import_user.makeUserFileForWelcomeMail").info("( * ) End.");
    }

    public void init() {
        StringBuffer sQuery = new StringBuffer()
                             .append("SELECT A.ID                              \n")
                             .append("     , A.PASSWORD_NEW                    \n")
                             .append("     , B.EMAIL                           \n")
                             .append("     , B.FNAME                           \n")
                             .append("     , B.LNAME                           \n")
                             .append("     , B.GENDER                          \n")
                             .append("  FROM G_MB_MEMBER  A                    \n")
                             .append("     , " + TABLE_NAME + " B              \n")
                             .append(" WHERE A.U_EMAIL = LOWER(B.EMAIL)        \n")
                             .append("   AND A.MEMBERGBN = '" + MEMBERGBN + "' \n");

        StringBuffer sbfLine = new StringBuffer();

        String id     = "";
        String passwd = "";
        String email  = "";
        String fname  = "";
        String lname  = "";
        String gender = "";

        OutputStreamWriter fw = null;

        int    i = 0;

        Connection conn = null;
        Statement  stmt = null;
        ResultSet rs    = null;

        try{
            TripleDES tplDES = new TripleDES(TRPLKEY);

            conn = ConnectionManager.getConnection();
            conn.setAutoCommit(true);
            stmt= conn.createStatement();

            fw = new OutputStreamWriter(new FileOutputStream("/home01/sfc/batch/import_user/data/file/" + FILE_NAME, true), "UTF-8");

            rs = stmt.executeQuery(sQuery.toString());

            while (rs.next()) {
                i++;

                id     = ObjectUtils.toString(rs.getObject(1));
                passwd = ObjectUtils.toString(rs.getObject(2));
                email  = ObjectUtils.toString(rs.getObject(3));
                fname  = ObjectUtils.toString(rs.getObject(4));
                lname  = ObjectUtils.toString(rs.getObject(5));
                gender = ObjectUtils.toString(rs.getObject(6));

                sbfLine.setLength(0);
                sbfLine.append(id    ).append("\t")
                       .append(email ).append("\t")
                       .append(fname ).append("\t")
                       .append(lname ).append("\t")
                       .append(gender).append("\t")
                       .append(STRURL)
                       .append("?p="          ).append(tplDES.encrypt(id + "|" + passwd))
                       .append("&t_url="      )
                       .append("&reg_country=").append(REG_COUNTRY)
                       .append("\n");

                fw.write(sbfLine.toString());

                //if (i == 500) break;

                if((i%500) == 0){
                    Logger.getLogger("import_user.makeUserFileForWelcomeMail").info("here we are == " + i);
                }
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
            Logger.getLogger("import_user.makeUserFileForWelcomeMail").info(ex.toString());
        }
        finally{
            if (fw != null)   try { fw.close();   } catch (Exception se) {}
            if (rs  != null)  try { rs.close();   } catch (Exception se) {}
            if (stmt != null) try { stmt.close(); } catch (Exception se) {}
            if (conn != null) try { conn.close(); } catch (Exception se) {}
        }
    }
}